<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let a = 7, b = 1

    // 1.利用ES6解构赋值
    function swapNumbers(a, b) {
      [a, b] = [b, a]
      return [a, b];
    }
    console.log(swapNumbers(a, b));

    // 2.利用加减法
    function swapNumbers2(a, b) {
      a = a + b
      b = a - b
      a = a - b
      return [a, b];
    }
    console.log(swapNumbers2(a, b));

    // 3.利用位运算(异或^)
    function swapNumbers3(a, b) {
      a ^= b
      b ^= a
      a ^= b
      return [a, b]
    }
    console.log(swapNumbers3(a, b));

  </script>
</body>

</html>